package com.withoutwords.ui;

import java.util.Locale;

import com.withoutwords.engine.Common;
import com.withoutwords.engine.MyInfo;
import com.withoutwords.engine.comminication.CommunicationManager;
import com.withoutwords.engine.comminication.IServerResultListener;
import com.withoutwords.engine.comminication.ServerRequests;
import com.withoutwords.engine.settings.Settings;
import com.withoutwords.engine.social.ISocialNetworkController;
import com.withoutwords.engine.social.SocialEngine;
import com.withoutwords.engine.social.SocialNetwork;
import com.withoutwords.engine.social.listeners.ICurrentUserInfoListener;
import com.withoutwords.engine.social.listeners.ILoginListener;
import com.withoutwords.ui.custom.DemoImageAdapter;
import com.withoutwords.ui.custom.MyCustomButton;
import com.withoutwords.R;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.Toast;

public class LoginActivity extends BaseActivity implements
		OnClickListener,
		ICurrentUserInfoListener,
		IServerResultListener,
		ILoginListener
{
	ISocialNetworkController fbController;
	Handler uiHandler;
	Gallery galleryLayout;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.login);
		Intent intent = getIntent();
		boolean bOpenHelp = intent.getBooleanExtra(Common.openHelp, false);
		
		ImageButton btn = (ImageButton) findViewById(R.id.btnLoginFb);
		if(bOpenHelp)
		{
			btn.setVisibility(View.INVISIBLE);
		}
		else
		{
			btn.setOnClickListener(this);
		}
		
		uiHandler = new Handler();
		
		galleryLayout = (Gallery) findViewById(R.id.demo_gallery);
		
		galleryLayout.setAdapter(new DemoImageAdapter(this));
		boolean RTL = Locale.getDefault().getLanguage().toLowerCase()
				.contains("ar");
		if(RTL)
			galleryLayout.setSelection(galleryLayout.getCount() -1);
		
		playSound(soundResId);
		
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		View v = findViewById(R.id.ListLayout);
		v.setVisibility(View.VISIBLE);
	}
	
	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
			case R.id.btnLoginFb:
			{
				fbController = SocialEngine.getSocialEngine()
						.getSocialNetworkController(SocialNetwork.Facebook);
				fbController.requestLogin(this, this);
				break;
			}
		}
	}
	
	@Override
	protected void setBitmaps()
	{
		super.setBitmaps();
		setLayoutBitmap(R.id.lyBlackboard, R.drawable.blackboard);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(fbController == null)
			fbController = SocialEngine.getSocialEngine()
			.getSocialNetworkController(SocialNetwork.Facebook);
		boolean bSuccess = fbController.onLoginCallback(requestCode, resultCode, data);
		/*
		 * if(bSuccess) { fbController.getCurrentUserInfo(this); }
		 */
		// finish();
	}
	
	@Override
	public void onError(final String Error)
	{
		uiHandler.post(new Runnable()
		{
			
			@Override
			public void run()
			{
				Toast.makeText(LoginActivity.this, Error, Toast.LENGTH_LONG).show();
				// finish();
			}
		});
	}
	
	MyInfo me = null;
	
	@Override
	public void onCurrentUserInfoRecieved(MyInfo _me)
	{
		me = _me;
		Settings sett = Settings.getInstance();
		
		Bundle data = new Bundle();
		data.putString("Email", "mona.shokrof@gmail.com");// me.getEmail());
		data.putString("socialID", me.getSocialUser().getSocialID());
		data.putString("platFormId", "1");
		data.putString("userName", me.getSocialUser().getName());
		data.putString("tokenId", sett.getPushToken());
		
		CommunicationManager.getCommnuicationManager().request(this,ServerRequests.registerUser,
				data,
				this);
		
	}
	
	@Override
	public void onRequestSucceed(ServerRequests serverRequest, Bundle response)
	{
		if (serverRequest == ServerRequests.registerUser)
		{
			String userId = response.getString(Common.userIdCode);
			Settings sett = Settings.getInstance();
			if (me != null)
			{
				me.setUserId(userId);
				sett.setMyInfo(me, true);
			}
			startActivity(new Intent(getApplicationContext(), WithoutWordsActivity.class));
			finish();
		}
		
	}
	
	@Override
	public void onRequestFailed(ServerRequests serverRequest, final String error)
	{
		uiHandler.post(new Runnable()
		{
			
			@Override
			public void run()
			{
				Toast.makeText(LoginActivity.this, error, Toast.LENGTH_LONG).show();
				// finish();
			}
		});
		
	}
	
	@Override
	public void onLoginSuccess()
	{
		fbController.getCurrentUserInfo(this);
		
	}
	
	@Override
	protected void unsetBitmaps()
	{
		// Auto-generated method stub
		super.unsetBitmaps();
		unsetLayoutBitmaps(R.id.lyBlackboard);
		
	}
}
